package com.neusoft.healthcity.config.scheduled;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neusoft.healthcity.config.ReadJsonConfig;
import com.neusoft.healthcity.config.utils.BeanUtil;

import com.neusoft.healthcity.entity.*;

import com.neusoft.healthcity.entity.json.*;
import com.neusoft.healthcity.service.*;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Component
public class JsonCeneratorPublicHealthEmergencies {

    @Autowired
    HevtReportsRebackService hevtReportsRebackService;
    @Autowired
    HealthEmergenciesRebackService healthEmergenciesRebackService;
    @Autowired
    EvtReportSourcesRebackService evtReportSourcesRebackService;
    @Autowired
    EvtReportOthSourcesRebackService evtReportOthSourcesRebackService;
    @Autowired
    EvtReportApparatusRebackService evtReportApparatusRebackService;
    @Autowired
    EvtAttachmentsRebackService evtAttachmentsRebackService;

    @Value("${jsonRead.healthEmergencies.jsonZipLocation}")
    private String zipPath;
    @Value("${jsonRead.healthEmergencies.jsonFileDescLocation}")
    private String descDir;

    private Logger logger = LoggerFactory.getLogger(JsonCeneratorPublicHealthEmergencies.class);

    private long startTime;
    private long endTime = 0;
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

    @Scheduled(cron = "0 */10 * * * ?")
    public void jsonPublicHealthEmergenciesScheduled() {
        logger.info("*************数据获取定时任务开始*************");
        try {
            startTime = new Date().getTime();
            logger.info("*************"+startTime+"*************");
            logger.info("*************"+endTime+"*************");
            ReadJsonConfig.unZipFiles(zipPath, descDir,startTime,endTime);
            ArrayList<File> fileArrayList = new ArrayList<>();
            ArrayList<File> files = ReadJsonConfig.getAllFile(descDir, fileArrayList);
            for (int i = 0; i < files.size(); i++) {
                File file = files.get(i);
                String input = FileUtils.readFileToString(file, "UTF-8");
                System.out.println(input);
                JSONObject jsonObject = JSON.parseObject(input);

                //创建表实体对象
                HealthEmergenciesReback healthEmergenciesReback = new HealthEmergenciesReback();
                HevtReportsReback hevtReportsReback = new HevtReportsReback();
                EvtReportSourcesReback evtReportSourcesReback = new EvtReportSourcesReback();
                EvtReportOthSourcesReback evtReportOthSourcesReback = new EvtReportOthSourcesReback();
                EvtReportApparatusReback evtReportApparatusReback = new EvtReportApparatusReback();
                EvtAttachmentsReback evtAttachmentsReback = new EvtAttachmentsReback();

                JSONObject jsonArray2 = jsonObject.getJSONObject("PublicHealthEmergencies");


                healthEmergenciesReback.setBusinessactivityidentification((jsonArray2.getString("BusinessActivityIdentification")));
                healthEmergenciesReback.setTotalrecords(jsonArray2.getString("TotalRecords"));
                healthEmergenciesReback.setStarttime(jsonArray2.getString("StartTime"));
                healthEmergenciesReback.setEndtime(jsonArray2.getString("EndTime"));

                JSONArray array = jsonObject.getJSONArray("PublicHealthEmergenciesList");
                for (int j = 0; j < array.size(); j++) {
                    JSONObject object = (JSONObject) array.get(j);
                    PublicHealthEmergenciesList deathRegList = JSONObject.toJavaObject(object, PublicHealthEmergenciesList.class);
                    BeanUtils.copyProperties(deathRegList, healthEmergenciesReback);
                    //EvtReports数组标签
                    JSONArray evtReports = object.getJSONArray("EvtReports");
                    for (int k = 0; k < evtReports.size(); k++) {
                        JSONObject object1 = (JSONObject) evtReports.get(k);
                        EvtReports evtReports1 = JSONObject.toJavaObject(object1, EvtReports.class);
                        BeanUtil.copyPropertiesIgnoreCase(evtReports1, hevtReportsReback);
                        hevtReportsReback.setCreatedate(new Date());
                        hevtReportsRebackService.save(hevtReportsReback);
                        //json标签EvtReportSources
                        JSONArray evtReportSources1 = object1.getJSONArray("EvtReportSources");
                        for (int l = 0; l < evtReportSources1.size(); l++) {
                            JSONObject object2 = (JSONObject) evtReportSources1.get(l);
                            EvtReportSources evtReportSources2 = JSONObject.toJavaObject(object2, EvtReportSources.class);
                            BeanUtil.copyPropertiesIgnoreCase(evtReportSources2, evtReportSourcesReback);
                            evtReportSourcesReback.setCreatedate(new Date());
                            evtReportSourcesRebackService.save(evtReportSourcesReback);
                        }
                        //json标签EvtReportApparatus
                        JSONArray evtReportApparatus = object1.getJSONArray("EvtReportApparatus");
                        for (int l = 0; l < evtReportApparatus.size(); l++) {
                            JSONObject object3 = (JSONObject) evtReportSources1.get(l);
                            EvtReportApparatus evtReportApparatus1 = JSONObject.toJavaObject(object3, EvtReportApparatus.class);
                            BeanUtil.copyPropertiesIgnoreCase(evtReportApparatus1, evtReportApparatusReback);
                            evtReportApparatusReback.setCreatedate(new Date());
                            evtReportApparatusRebackService.save(evtReportApparatusReback);
                        }
                        //json标签EvtReportOthSources
                        JSONArray evtReportOthSources = object1.getJSONArray("EvtReportOthSources");
                        for (int l = 0; l < evtReportOthSources.size(); l++) {
                            JSONObject object4 = (JSONObject) evtReportSources1.get(l);
                            EvtReportOthSources evtReportOthSources1 = JSONObject.toJavaObject(object4, EvtReportOthSources.class);
                            BeanUtil.copyPropertiesIgnoreCase(evtReportOthSources1, evtReportOthSourcesReback);
                            evtReportOthSourcesReback.setCreatedate(new Date());
                            evtReportOthSourcesRebackService.save(evtReportOthSourcesReback);
                        }
                        //json标签EvtAttachments
                        JSONArray evtAttachments = object1.getJSONArray("EvtAttachments");
                        for (int l = 0; l < evtAttachments.size(); l++) {
                            JSONObject object5 = (JSONObject) evtReportSources1.get(l);
                            EvtAttachments evtAttachments1 = JSONObject.toJavaObject(object5, EvtAttachments.class);
                            BeanUtil.copyPropertiesIgnoreCase(evtAttachments1, evtAttachmentsReback);
                            evtAttachmentsReback.setCreatedate(new Date());
                            evtAttachmentsRebackService.save(evtAttachmentsReback);
                        }
                    }

                }
                healthEmergenciesReback.setCreatedate(new Date());
                healthEmergenciesRebackService.save(healthEmergenciesReback);
            }
            endTime = startTime;
            //删除解压目录
            File file = new File(descDir);
            ReadJsonConfig.deleteDir(file);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
